package sdfi.lee.Entity;

import java.io.File;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import sdfi.lee.DAO.NoticeDAO;
import sdfi.lee.Entity.base.BaseNotice;



public class Notice extends BaseNotice {
	private static final long serialVersionUID = 1L;
	private  File[] files=null;
	private  String[] filesContentType=null;
	private String[]  filesFileName=null;
	private String[] noticeids=null;
	private String date=null;
/*[CONSTRUCTOR MARKER BEGIN]*/
	public Notice () {
		super();
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	/**
	 * Constructor for primary key
	 */
	public Notice (java.lang.Integer id) {
		super(id);
	}

	/**
	 * Constructor for required fields
	 */
	public Notice (
		java.lang.Integer id,
		java.lang.String noticetitle,
		java.util.Date noticedate) {

		super (
			id,
			noticetitle,
			noticedate);
	}
	public String delete()
	{
		String path=ServletActionContext.getRequest().getRealPath("/noticefile");
		return new NoticeDAO().deleteNotice(this.noticeids,path);
	}
	public String execute()
	{
		try
		{	
			this.upfile();
			this.setNoticedate(new Date());
			return new NoticeDAO().addNotice(this);
		}
		catch(Exception ee)
		{
			return "error";
		}
	}
	
	public String modify()
	{
		String path=ServletActionContext.getRequest().getRealPath("/noticefile");
		try
		{
		this.upfile();
		return new NoticeDAO().modifyNotice(this,path);
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
			return "error";
		}
		
	}
	private void upfile() throws Exception 
	{
		try
		{
		if(this.files!=null&&this.files.length>0)
		{
			File file=null;
			String path=ServletActionContext.getRequest().getRealPath("/noticefile");
			for(int i=0;i<files.length;i++)
			{
				file=new File(path,filesFileName[i]);
				FileUtils.copyFile(files[i],file);
			}
		}
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
		}
	}
/*[CONSTRUCTOR MARKER END]*/

	public File[] getFiles() {
		return files;
	}

	public void setFiles(File[] files) {
		this.files = files;
	}

	public String[] getFilesContentType() {
		return filesContentType;
	}

	public void setFilesContentType(String[] filesContentType) {
		this.filesContentType = filesContentType;
	}

	public String[] getFilesFileName() {
		return filesFileName;
	}

	public void setFilesFileName(String[] filesFileName) {
		this.filesFileName = filesFileName;
	}

	public String[] getNoticeids() {
		return noticeids;
	}

	public void setNoticeids(String[] noticeids) {
		this.noticeids = noticeids;
	}


}